package main

import (
	"bytes"
	"fmt"
	"strings"
	"unicode/utf8"
)

func main() {
	text1 := "\"Hello Golang\", I said"
	text2 := `"Hello world", he said`
	fmt.Println(text1)
	fmt.Println(text2)

	text3 := "Hello" + " Golang"
	text3 += " PHP"
	fmt.Println(text3)

	text4 := "abcdefg"
	fmt.Println(text4[1])
	fmt.Println(string(text4[1]))
	fmt.Println(text4[1:3])
	fmt.Println(text4[5:])
	fmt.Println(text4[:3])
	fmt.Println(len(text4))
	fmt.Println(utf8.RuneCountInString(text4))
	fmt.Println([]rune(text4)) // [97 98 99 100 101 102 103]
	fmt.Println([]rune("大家好")) // [22823 23478 22909]

	// 字符与字符串
	var bf bytes.Buffer
	bf.WriteString("a")
	bf.WriteString("b")
	bf.WriteString("c")

	fmt.Println(bf.String())

	// 字符串不可变
	text5 := "hello world"
	b := []rune(text5)
	b[0] = 'H'
	text5 = string(b)
	fmt.Printf("%v->type: %T\n", text5, text5)

	// strings.Contains
	fmt.Println(strings.Contains(text5, "ll"))
	// strings.Count
	fmt.Println(strings.Count(text5, "l"))
	fmt.Println(strings.Count(text5, "o"))
	fmt.Println(strings.Count(text5, "O"))
	fmt.Println(strings.Count(strings.ToUpper(text5), "O"))
	// strings.EqualFold
	fmt.Println(strings.EqualFold(text5, "Hello World"))
	// strings.Index
	fmt.Println(strings.Index("hello,golang", ","))
	fmt.Println(strings.LastIndex("hello,golang", "g"))
	// strings.Replace
	fmt.Println(strings.Replace("he11o,golang", "1", "l", 1))
	fmt.Println(strings.Replace("he11o,golang", "1", "l", 2))
	// strings.Split
	arr := strings.Split("hello,world,nice,day", ",")
	fmt.Println(arr)
	fmt.Printf("%T \n%v\n", arr, arr[2])

	// strings.Join
	fmt.Println(strings.Join(arr, ","))

	// strings.ToUpper
	fmt.Println(strings.ToUpper("abc"))
	fmt.Println(strings.ToLower("FFF"))
	// strings.TrimSpace
	// 中间的空格不会被去掉
	fmt.Println(strings.TrimSpace("      66 6    "))
	// strings.HasPrefix
	fmt.Println(strings.HasPrefix("666777", "666"))
	fmt.Println(strings.HasSuffix("666777", "777"))
	// strings.ContainsAny
	fmt.Println("team strings.ContainsAny i ", strings.ContainsAny("team", "i"))
	fmt.Println("failure strings.ContainsAny a & o ", strings.ContainsAny("failure", "a & o"))
	fmt.Println("foo strings.ContainsAny \"\" ", strings.ContainsAny("foo", ""))
	fmt.Println("\"\" strings.ContainsAny \"\" ", strings.ContainsAny("", ""))
	fmt.Println("team strings.Contains i", strings.Contains("team", "i"))
	fmt.Println("failure strings.Contains a & o", strings.Contains(" failure", "a & o"))
	fmt.Println("foo strings.Contains \"\"", strings.Contains("foo", ""))
	fmt.Println("\"\" strings.Contains \"\" ", strings.Contains("", ""))

	// strings.Replace
	fmt.Println(strings.Replace("Hello world", "Hello", "Hi", 1))

	// strings.Trim
	fmt.Printf("%q\n", strings.Trim(" ??? Golang ??? ", "? "))
	fmt.Printf("%q\n", strings.Trim(" ??? Golang ??? ", " ? "))
	fmt.Printf("%q\n", strings.Trim(" ??? Golang ??? ", "?"))
	// strings.TrimLeft
	fmt.Printf("%q\n", strings.TrimLeft(" ??? Golang ??? ", "? "))
	fmt.Printf("%q\n", strings.TrimLeft(" ??? Golang ??? ", " ? "))
	fmt.Printf("%q\n", strings.TrimLeft(" ??? Golang ??? ", "?"))

}
